package com.sishu.njrtsdms.manager.factory;

import com.sishu.njrtsdms.constant.Constants;
import com.sishu.njrtsdms.enums.DictDataEnum;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.LoginInfoDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.pojos.LoginInfo;
import com.sishu.njrtsdms.manager.DictionaryManager;
import com.sishu.njrtsdms.utils.LogUtils;
import com.sishu.njrtsdms.utils.ServletUtils;
import com.sishu.njrtsdms.utils.ip.AddressUtils;
import com.sishu.njrtsdms.utils.ip.IpUtils;

import java.time.LocalDateTime;
import java.util.TimerTask;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

/**
 * 异步工厂（产生任务用）
 *
 * @author njrts-dms
 */
@Slf4j(topic = "sys-user")
public class AsyncFactory {

    public static final AsyncFactory INSTANCE = new AsyncFactory();


    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息
     * @param args     列表
     * @return 任务task
     */
    public TimerTask recordLoginInfo(final String username,
                                     final String status,
                                     final String message,
                                     final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(
                ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr();
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                LoginInfo loginInfo = new LoginInfo();
                loginInfo.setUsername(username);
                loginInfo.setIpaddr(ip);
                loginInfo.setLoginLocation(address);
                loginInfo.setBrowser(browser);
                loginInfo.setOs(os);
                loginInfo.setMsg(message);
                loginInfo.setLoginDateTime(LocalDateTime.now());
                // 日志状态
                var dictionaryManager = SpringUtil.getBean(DictionaryManager.class);
                if (StrUtil.equalsAny(status,
                        Constants.LOGIN_SUCCESS,
                        Constants.LOGOUT,
                        Constants.REGISTER)) {
                    loginInfo.setLoginStatusDict(dictionaryManager
                            .getIdByEnum(DictDataEnum.LOGIN_SUCCESS));
                } else if (Constants.LOGIN_FAIL.equals(status)) {
                    loginInfo.setLoginStatusDict(dictionaryManager
                            .getIdByEnum(DictDataEnum.LOGIN_FAIL));
                }
                // 插入数据

                SpringUtil.getBean(LoginInfoDAO.class).insertSelective(loginInfo);
            }
        };
    }

//    /**
//     * 操作日志记录
//     *
//     * @param operLog 操作日志信息
//     * @return 任务task
//     */
//    public static TimerTask recordOper(final SysOperLog operLog) {
//        // // TODO: 2023/7/19
//        return new TimerTask() {
//            @Override
//            public void run() {
//                // 远程查询操作地点
//                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
//                SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
//            }
//        };
//    }
}
